﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using trangbaocao.Models;
using System.Collections;

namespace trangbaocao.Controllers
{
    public class Manage_TruyenDanController : Controller
    {
        private Manage_TruyendanModels manage_truyendan = new Manage_TruyendanModels();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        public ActionResult Index()
        {
            List<DeviceTruyenDan> listTruyendan = manage_truyendan.getAllTruyendan();
            return View(listTruyendan);
        }
        public ActionResult Device_Stations()
        {
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            List<Device_Stations_Parament_Using> list = manage_truyendan.truyendan_stations_pra_us();
            return View(list);
        }

        [HttpPost]
        public ActionResult Fill_Stations()
        {
            string trungtam_id = Request.Form["main_stations_id"];
            string tram_id = Request.Form["stations_id"];
            string sql = "";
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            if (trungtam_id != "" && trungtam_id != null && IsNumber(trungtam_id))
                sql = " and a.main_stations_id =" + Convert.ToInt32(trungtam_id);
            if (tram_id != "" && tram_id != null && IsNumber(tram_id))
                sql = " and a.stations_id =" + Convert.ToInt32(tram_id);
            List<Device_Stations_Parament_Using> list = manage_truyendan.truyendan_stations_pra_us_where(sql);
            return View("Device_Stations", list);
        }
        private bool IsNumber(string pValue)
        {
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }
        public ActionResult Parameter_TruyenDan(int device_id)
        {
            List<Paramenter_Base_TruyenDan> list_paramenter_base = manage_truyendan.getParamentBase(device_id);
            ViewData["truyendan_id"] = device_id;
            ViewBag.NameTD = manage_truyendan.getNameTD(device_id);
            return View(list_paramenter_base);
        }
        public ActionResult Del_Parameter_Base(int pra_id, int truyendan_id)
        {
            manage_truyendan.DeleteParameterBaseTruyenDan(pra_id);
            return RedirectToAction("Parameter_TruyenDan", new { device_id = truyendan_id });
        }
        public ActionResult Add_Parameter_Base(int truyendan_id)
        {
            ViewBag.truyendan_id = truyendan_id;
            ViewBag.NameTD = manage_truyendan.getNameTD(truyendan_id);
            return View();
        }
        public ActionResult Edit_Parameter_Base(int para_id)
        {
            Paramenter_Base_TruyenDan para_base = manage_truyendan.getByParamenter_Base_id(para_id);
            ViewBag.NameTD = manage_truyendan.getNameTD(para_base.device_id);
            ViewBag.truyendan_id = para_base.device_id;
            return View(para_base);
        }
        [HttpPost]
        public ActionResult Edit_Parameter_Base(Paramenter_Base_TruyenDan para_base)
        {
            if (ModelState.IsValid)
            {
                manage_truyendan.UpdateParameterBaseTruyenDan(para_base);
                TempData["msg"] = "Bạn đã cập nhật thành công!";
                return RedirectToAction("Parameter_TruyenDan", new { device_id = para_base.device_id });
            }
            else 
            {
                TempData["msg"] = "Bạn đã nhập sai dữ liệu !";
                ViewBag.truyendan_id = para_base.device_id;
                return View();
            }
        }
        [HttpPost]
        public ActionResult Add_Parameter_Base(Paramenter_Base_TruyenDan truyendanbase, int truyendan_id)
        {

            DeviceTruyenDan dtrd = manage_truyendan.getDeviceTruyenDanById(truyendan_id);
            if (ModelState.IsValid)
            {
                if (manage_truyendan.checkExist(truyendanbase.name_paramenter, truyendan_id) > 0)
                {
                    TempData["msg"] = "Thuộc tính của thiết bị đã tồn tại!";
                    return RedirectToAction("Add_Parameter_Base", new { truyendan_id = truyendan_id });
                }
                else
                {
                    truyendanbase.device_id = dtrd.device_id;
                    truyendanbase.main_sort_device_id = dtrd.main_sort_device_id;
                    truyendanbase.sort_device_id = dtrd.sort_device_id;
                    truyendanbase.manufactorys_id = dtrd.manufactorys_id;
                    truyendanbase.pra_meba_sort_dv_id = 0;
                    manage_truyendan.AddParameterBaseTruyenDan(truyendanbase);
                    TempData["msg"] = "Bạn đã cập nhật thành công!";
                    return RedirectToAction("Parameter_TruyenDan", new { device_id = truyendan_id });
                }
            }
            else
            {
                TempData["msg"] = "Bạn đã nhập sai dữ liệu !";
                ViewBag.truyendan_id = truyendan_id;
                return View();
            }
        }
        public ActionResult Parameter_Using_TruyenDan(int device_stations_id)
        {
            List<Paramenter_Using_TruyenDan> list = manage_truyendan.getParamenterUsingTruyenDan(device_stations_id);
            ViewBag.device_stations_id = device_stations_id;
            ViewBag.NameDeviceStation = manage_truyendan.getNameDeviceStation(device_stations_id);
            return View(list);
        }
        public ActionResult Add_Parameter_Using(int device_stations_id)
        {

            ViewBag.device_stations_id = device_stations_id;
            Device_Stations_Parament_Using dvs = manage_truyendan.truyendan_stations_pra_us_id(device_stations_id);
            ViewBag.device_id = dvs.device_id;
            ViewBag.NameDeviceStation = manage_truyendan.getNameDeviceStation(device_stations_id);
            List<Paramenter_Base_TruyenDan> para = manage_truyendan.getByParamenter_Base_device_id(Convert.ToInt32(dvs.device_id.ToString()));
            ViewBag.Parabase = new SelectList(para, "paramenter_base_id", "name_paramenter");
            return View();
        }
        [HttpPost]
        public ActionResult Add_Parameter_Using(Paramenter_Using_TruyenDan pra, int device_stations_id)
        {
            Device_Stations device_station = manage_truyendan.getDeviceStationsById(device_stations_id);
            if (ModelState.IsValid)
            {
                if (manage_truyendan.checkPraUsingExist(pra.paramenter_base_id, device_stations_id) > 0)
                {
                    TempData["msg"] = "Tham số của thiết bị đã tồn tại!";
                    return RedirectToAction("Add_Parameter_Using", new { device_stations_id = device_stations_id });
                }
                
                    pra.device_id = device_station.device_id;
                    pra.main_sort_device_id = device_station.main_sort_device_id;
                    pra.sort_device_id = device_station.sort_device_id;
                    pra.manufactorys_id = device_station.manufactorys_id;
                    pra.device_stations_id = device_station.device_station_id;
                    pra.date_setup = DateTime.Now.ToString("dd/MM/yyyy");
                    pra.date_sys = DateTime.Now.ToString("dd/MM/yyyy");
                    manage_truyendan.AddParamenterUsingTruyenDan(pra);
                    TempData["msg"] = "Bạn đã cập nhật thành công!";
                    return RedirectToAction("Parameter_Using_TruyenDan", new { device_stations_id = device_stations_id });
               
            }
            else
            {
                ViewBag.device_stations_id = device_stations_id;
                Device_Stations_Parament_Using dvs = manage_truyendan.truyendan_stations_pra_us_id(device_stations_id);
                ViewBag.device_id = dvs.device_id;
                List<Paramenter_Base_TruyenDan> para = manage_truyendan.getByParamenter_Base_device_id(Convert.ToInt32(dvs.device_id.ToString()));
                ViewBag.Parabase = new SelectList(para, "paramenter_base_id", "name_paramenter");
                TempData["msg"] = "Bạn đã nhập sai số liệu!";
                return View();
            }
        }
        public ActionResult Delete_Parameter_Using(int id, int device_stations_id)
        {
            manage_truyendan.DeleteParamenterUsingTruyenDan(id);
            ViewBag.device_stations_id = device_stations_id;
            return RedirectToAction("Parameter_Using_TruyenDan", new { device_stations_id = device_stations_id });
        }
        public ActionResult Edit_Parameter_Using(int parid)
        {
            Paramenter_Using_TruyenDan pra = manage_truyendan.getParameterUsingById(parid);
            List<Paramenter_Base_TruyenDan> para = manage_truyendan.getList_ByParamenter_Base_id(pra.paramenter_base_id);
            ViewBag.Parabase = new SelectList(para, "paramenter_base_id", "name_paramenter");
            ViewBag.device_stations_id = pra.device_stations_id;
            ViewBag.NameDeviceStation = manage_truyendan.getNameDeviceStation(pra.device_stations_id);
            ViewBag.device_id = pra.device_id;
            return View(pra);
        }
        [HttpPost]
        public ActionResult Edit_Parameter_Using(Paramenter_Using_TruyenDan par, int device_stations_id)
        {
            if (ModelState.IsValid)
            {
                
                    par.date_setup = DateTime.Now.ToString("dd/MM/yyyy");
                    par.date_sys = DateTime.Now.ToString("dd/MM/yyyy");
                    manage_truyendan.Update_Parabase_Using_TruyenDan(par);
                    TempData["msg"] = "Bạn đã cập nhật số liệu thành công!";
                    return RedirectToAction("Parameter_Using_TruyenDan", new { device_stations_id = device_stations_id });
               
            }
            else
            {
                Paramenter_Using_TruyenDan pra = manage_truyendan.getParameterUsingById(par.paramenter_using_id);
                List<Paramenter_Base_TruyenDan> para = manage_truyendan.getList_ByParamenter_Base_id(pra.paramenter_base_id);
                ViewBag.Parabase = new SelectList(para, "paramenter_base_id", "name_paramenter");
                ViewBag.device_stations_id = pra.device_stations_id;
                ViewBag.device_id = pra.device_id;
                TempData["msg"] = "Bạn đã nhập sai số liệu!";
                return View();
            }
        }
        public ActionResult Device_Configuration_Report_Index()
        {
            return View("~/Views/Manage_TruyenDan/Report/Device_Configuration_Report_Index.cshtml");
        }
    }
}
